﻿using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Threading.Tasks;
using RuoVea.ExEnum;
using RuoVea.ExSqlSugar;
using RuoVea.ExUtil.Exceptions;
using RuoVea.ExUtil.EnumExtension;
using RuoVea.Autowired;
using Microsoft.Extensions.DependencyInjection;
using RuoVea.Util;
using RuoVea.Entity;

namespace RuoVea.Service
{
    /// <summary>
    /// 职位服务
    /// </summary>
    [AppService(ServiceLifetime.Transient)]
    public class SysPosService : ISysPosService
    {
        private readonly ISqlSugarRepository<SysPos> _sysPosRep;  // 职位表仓储   


        public SysPosService(ISqlSugarRepository<SysPos> sysPosRep )
        {
            _sysPosRep = sysPosRep;
        }

        /// <summary>
        /// 分页获取职位
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<dynamic> QueryPosPageList(PosInput input)
        {
            var pos = await _sysPosRep.Context.Queryable<SysPos>()
                                    .WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name.Trim()))
                                    .WhereIF(!string.IsNullOrWhiteSpace(input.Code), u => u.Code.Contains(input.Code.Trim()))
                                    .Where(u => u.Status == StatusEnum.ENABLE).OrderBy(u => u.Sort)
                                    .ToPagedListAsync(input.PageNo, input.PageSize);
            return pos.XnPagedResult();
        }

        /// <summary>
        /// 获取职位列表
        /// </summary>
        /// <returns></returns>
        public async Task<dynamic> GetPosList(PosInput input)
        {
            return await _sysPosRep.Context.Queryable<SysPos>()
                                                     .WhereIF(!string.IsNullOrWhiteSpace(input.Code), u => u.Code.Contains(input.Code.Trim()))
                                                     .Where(u => u.IsDeleted==false)
                                                     .OrderBy(u => u.Sort).ToListAsync();
        }

        /// <summary>
        /// 增加职位
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task AddPos(AddPosInput input)
        {
            var isExist = await _sysPosRep.AnyAsync(u => u.Name == input.Name || u.Code == input.Code);
            if (isExist)
                throw new ParamiterException(ErrorCode.D6000.GetEnumText());

            var pos = input.Adapt<SysPos>();
            await _sysPosRep.Context.Insertable(pos).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
        }

        /// <summary>
        /// 删除职位
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task DeletePos(DeletePosInput input)
        {
            await _sysPosRep.DeleteAsync(u => u.Id == input.Id);
        }

        /// <summary>
        /// 更新职位
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task UpdatePos(UpdatePosInput input)
        {
            var isExist = await _sysPosRep.AnyAsync(u => (u.Name == input.Name || u.Code == input.Code) && u.Id != input.Id);
            if (isExist)
                throw new ParamiterException(ErrorCode.D6000.GetEnumText());

            var pos = input.Adapt<SysPos>();
            await _sysPosRep.Context.Updateable(pos).IgnoreColumns(ignoreAllNullColumns: true).CallEntityMethod(m=>m.Modify()).ExecuteCommandAsync();
        }

        /// <summary>
        /// 获取职位
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<SysPos> GetPos(QueryPosInput input)
        {
            return await _sysPosRep.FirstOrDefaultAsync(u => u.Id == input.Id);
        }
    }
}
